﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using RPCenter.UpdaterUtil;
using System.IO;
using System.ServiceProcess;

namespace RPCenter.UpdaterUtil
{
    /// <summary>
    /// 启动进程、关闭进程操作
    /// </summary>
    public class OperProcess
    {
        Config config = Config.LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstFile.FILENAME)); 
        #region init update env
        public void InitUpdateEnvironment()
        {
            if (config.MainProgressType == "exe")
            {
                if (IfExist(config.MainProgressName))
                {
                    CloseExe(config.MainProgressName);
                }
            }
            else
            {

                if (IsServiceIsExisted(config.MainProgressName))
                {
                    StartStopService(config.MainProgressName, 2);
                }
            }
        }
        #endregion init update env



        #region updated start process
        public void StartProcess()
        {
            string path= config.MainProgressPath;
            if (string.IsNullOrEmpty(config.MainProgressPath))
            {
                path = System.Environment.CurrentDirectory;
            }
            if (config.MainProgressType == "exe")
            {
                if (!IfExist(config.MainProgressName))
                {
                    StartExe(path, config.MainProgressExe);
                }
            }
            else
            {
                if(IsServiceIsExisted(config.MainProgressName))
                {
                    StartStopService(config.MainProgressName, 1);
                }
            }
            CloseExe("RPCenter.Updater");
        }

        #endregion

        #region 服务启动 停止
        /// <summary>
        /// 服务启动 停止
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="theEvent">1启动 2停止</param>
        private void StartStopService(string serviceName,int theEvent)
        {
            ServiceController serviceController1 = new ServiceController();
            serviceController1.ServiceName = serviceName;
            serviceController1.MachineName = "."; 
            if (theEvent == 1)
            {
                if (serviceController1.Status == ServiceControllerStatus.Stopped)
                {
                    serviceController1.Start();
                }
            }
            else
            {
                if (serviceController1.Status == ServiceControllerStatus.Running)
                    serviceController1.Stop();
            } 
        }
        #endregion

        #region 启动进程、关闭进程、判断进程是否存在
        //启动exe绝对路径
        private void StartExe(string filePath, string fileName)
        {
            Process proc = new Process();
            proc.StartInfo.UseShellExecute = false;//是否使用操作系统外壳程序启动进程

            proc.StartInfo.WorkingDirectory = filePath;//启动进程的初始目录
            proc.StartInfo.FileName = fileName;
            proc.Start();
        }


        //exeName 关闭的exe进程名
        private void CloseExe(string exeName)
        {
            Process[] arrPro = Process.GetProcessesByName(exeName);
            foreach (Process pro in arrPro)
                pro.Kill();
        }
        //processName 进程名
        private bool IfExist(string processName)
        {
            Process[] pro = Process.GetProcessesByName(processName);
            return pro.Count() > 0;
        }
        /// <summary>
        /// 检查服务是否存在
        /// </summary>
        /// <param name="NameService"></param>
        /// <returns></returns>
        public static bool IsServiceIsExisted(string NameService)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController s in services)
            {
                if (s.ServiceName.ToLower() == NameService.ToLower())
                {
                    return true;
                }
            }
            return false;
        }
        #endregion 启动进程、关闭进程
    }
}
